<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<%@taglib prefix="s" uri="/struts-tags"  %>
<%
	String operation = request.getParameter("operation");
%>
<%@ page import="com.starbaba.core.web.constant.WebConstants"%>
<%@ page import="com.starbaba.system.user.model.UserVO"%>
<!DOCTYPE html>
<html lang="en">
<head>
<%@ include file="/common/common-css.jsp"%>
<style>
.table>th {
	vertical-align: center;
	background-color: grey;
}
input[type="radio"] {
   font-size: 100px;
   vertical-align:middle;
   margin-top:-2px; 
   margin-left:2px;
   margin-bottom:1px;
}
</style>
</head>
<body class="no-skin">
	<div class="page-content">
		<div class="page-content-area">
			
			
			<!--  -->
			<div class="row">
				<div class="col-xs-12 col-sm-12 widget-container-col">
					<div class="widget-box widget-color-blue">
						<!-- #section:custom/widget-box.options.collapsed -->
						<div class="widget-header widget-header-small">
							<h6 class="widget-title lighter">
								<i class="ace-icon glyphicon glyphicon-th-list"></i>一级项名：${paramsMap.item.title}
							</h6>
                            <div class="widget-toolbar no-border">
								<a href="#" data-action="collapse">
									<i class="ace-icon fa fa-chevron-up"></i>
								</a>
							</div>
						</div>
						
						<div class="widget-body">
							<div class="widget-main">
								<form class="form-horizontal" id="queryForm" method="post" enctype="multipart/form-data">
									<input type="hidden" id="itemId" name="itemId" value="${paramsMap.item.id}"/>  
									<input type="hidden" id="index" name="index" value="${paramsMap.optionCount}"/>  
									<table id="baseinfo" class="table table-bordered table-striped table-hover table-condensed table-responsive table">
										<tbody id="optionTable">
	                                        <tr>
												<th>二级项ID</th>
												<th>二级可选项</th>
												<th>二级选项key名</th>
												<th>额外附加值</th>
												<th>操作</th>
											</tr>
											<c:forEach items="${paramsMap.optionList}" var="item" varStatus="status">
											<tr id="option${status.index+1}">
												<td>
													<span>${item.id}</span>
													<input type="hidden" id="optionId${status.index+1}" name="optionId${status.index+1}" value="${item.id}"/>
												</td>
												<td>
													<input type="text" name="optionValue${status.index+1}" id="optionValue${status.index+1}" placeholder="" value="${item.option_value}" class="col-xs-12 col-sm-4" style="width:200px;">
												</td>
												<td>
													<input type="text" name="optionKey${status.index+1}" id="optionKey${status.index+1}" placeholder="" value="${item.option_key}" class="col-xs-12 col-sm-4" style="width:200px;">
												</td>
												<td>
													<input type="text" name="extraValue${status.index+1}" id="extraValue${status.index+1}" placeholder="" value="${item.extra_value}" class="col-xs-12 col-sm-4" style="width:420px;">
												</td>
												<td>
													<a class="btn btn-success btn-sm" href="javascript:void(0)" onclick="deleteOption(${item.id},${status.index+1})">删除</a>
												</td>
											</tr>
											</c:forEach>
										</tbody>
									</table>
								</form>
									
							</div><!-- /.widget-main -->
							<div style="margin: 10px;">
								<button type="button" name="btn-add" class="btn btn-primary btn-sm no-border" onclick="addOption()">
									<i class="ace-icon glyphicon glyphicon-plus bigger-110"></i>添加二级项
								</button>
							</div>	
						</div><!-- /.widget-body -->
						
					</div><!-- /.widget-box -->
				</div><!-- /.col-sm-12 -->
			</div>
			
			
			
			<div class="clearfix form-actions">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn  btn-inverse  no-border " onclick="doCancle()"
						id="delete">
						<i class="ace-icon fa fa-undo bigger-160"></i> 取消
					</button>
					&nbsp; &nbsp; &nbsp;
					<button id="btn-save" name="btn-save"
						class="btn  btn-success  no-border" type="button"
						onclick="doSave()">
						<i class="ace-icon fa fa-floppy-o bigger-160"></i> 保存
					</button>
				</div>
			</div>
		</div>
	</div>

	<%@ include file="/common/common-js.jsp"%>
	<%@ include file="/discovery/maxlength.jsp"%>
	<script type="text/javascript" src="${ctx}/ace/js/maxlength.js"></script>
	<script type="text/javascript" src="${ctx}/js/wangEditor-1.3.11.min.js"></script>
	<script type="text/javascript" src="${ctx}/js/jquery.form.js"></script>
	<script type="text/javascript" src="${ctx}/js/call_func.js"></script>
	<script>
		var $multiple = 1;
		var $selector = '';
		var $input = '';

		$(function() {
			$('#img').change(function() {
				var file = this.files[0];
				var r = new FileReader();
				r.readAsDataURL(file);
				$(r).load(function() {
					$('#thumbnail').attr('src', this.result);
					$('#img').attr("value", this.result);
				});
			});

			//点击按钮
			$('.get-img-modal').on('click', function() {
				//展示模态框
				var $this = $(this);
				var $title = $this.attr('data-title');
				$input = $this.attr('data-input');
				$selector = $this.attr('data-selector');
				$multiple = $this.attr('data-multiple');
				if ($multiple == 1) {
					$('input[name="myFile"]').prop('multiple', true);
				} else {
					$('input[name="myFile"]').prop('multiple', false);
				}
				$('#Modal').find('h4.modal-title').html($title);
				$('#Modal').modal('show');
				return false;
			});

			$('.thumbnail').on('click', function() {
				$('#thumbnail').attr('src', "");
				$("input[id=thumbnail]").val("");
			});

		});
		
		function addOption(){
			var optionTable = $("#optionTable");
			var index = $("#index").val();
			index++;
			var html = "";
			html='<tr id="option'+index+'">'
				+	'<td>'
				+	'</td>'
				+	'<td>'
				+		'<input type="text" name="optionValue'+index+'" id="optionValue'+index+'" class="col-xs-12 col-sm-4" style="width:200px;">'
				+	'</td>'
				+	'<td>'
				+		'<input type="text" name="optionKey'+index+'" id="optionKey'+index+'" class="col-xs-12 col-sm-4" style="width:200px;">'
				+	'</td>'
				+	'<td>'
				+		'<input type="text" name="extraValue'+index+'" id="extraValue'+index+'" class="col-xs-12 col-sm-4" style="width:420px;">'
				+	'</td>'
				+	'<td>'
				+		'<a class="btn btn-success btn-sm" href="javascript:void(0)" onclick="deleteOption('+"''"+','+index+')">删除</a>'
				+	'</td>'
				+'</tr>';
			$("#index").val(index);
			optionTable.append(html);
		}

		function deleteOption(optionId,index){
			var optionTab = $("#option"+index);
			var answer = window.confirm("是否确认删除?");
			if(answer){
				if(isBlock(optionId)){  //临时添加的option
					optionTab.remove();
				}else{  //先删除数据库数据，再删除页面option
					var data = {"optionId":optionId};
					$.ajax({
						type:"post",	
						async:true,
						data:data,
						url: "${ctx}/apiModelBasisConf/deleteOption.action",
						success:function(resp){
							optionTab.remove();
						},
						error:function(error){
							alert("删除失败！");
						}
					});
				}
			}
		}

		function doCancle() {
			try {
				if (parent) {
					parent.parent['__extdialog_active'].dialog('close');
				}
			} catch (e) {
				window.history.back();
			}
		}
		
		function doSave() {
			var flag = false; 
			var optionValTab = $("input[id^='optionValue']");
			optionValTab.each(function(index,el){
				var $this = $(el);
				var val = $this.val();
				if(isBlock(val)){
					flag = true;
					return;
				}
			});
			if(flag){
				parent.bootbox.alert("请输入二级可选项");
				return;
			}
			
			var optionKeyTab = $("input[id^='optionKey']");
			optionKeyTab.each(function(index,el){
				var $this = $(el);
				var val = $this.val();
				if(isBlock(val)){
					flag = true;
					return;
				}
			});
			if(flag){
				parent.bootbox.alert("请输入二级选项key名");
				return;
			}
			
			saveForm();
		}

		function saveForm() {
			var saveUrl = "${ctx}/apiModelBasisConf/saveOptionList.action"; 
			var options = {
				url : saveUrl,
				type : "POST",
				dataType : "json",
				beforeSend : function() {
					$(".load_bg,.load_img").show();
				},
				success : function(data) {
					parent.parent['__extdialog_active'].dialog('close');
					parent.query_any();
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert(textStatus);
					console.log(XMLHttpRequest, textStatus, errorThrown);
				},
				complete : function() {
					$(".load_bg,.load_img").hide();
				}
			};
			$("#queryForm").ajaxSubmit(options);
		}
		
		function isBlock(value){
			if(value==""||value==null||value==undefined){
				return true;
			}
			return false;
		}
	</script>
</body>
</html>
